import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../views/index'
import home from '../views/home'
import category from '../views/category'
import cart from '../views/cart'
import me from '../views/me'
import login from '../views/me/login.vue'
Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        component: index,
        redirect: '/home',
        children: [
            { path: 'home', component: home,meta: {title: '商城'}},
            { path: 'category', component: category,meta: {title: '商品分类'}},
            { path: 'cart', component: cart,meta: {title: '购物车'}},
            { path: 'me', component: me,meta: {title: '个人中心',auth: true}},
        ]
    },
    {
        path: '/login',
        component: login,
        props: true
    }
]

let router = new VueRouter({
    routes
})
router.beforeEach((to,from,next) => {
    if (to.meta.auth) {
        if (!sessionStorage.getItem('auth')) {
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            })
            return;
        }
    }
    next()
})
export default router;